<?php
namespace Cms\Controller;
use Cms\Controller\BaseController;
class DeptController extends BaseController {
	
	public function _initialize() {
		//你可以在此覆盖父类方法	
		parent::_initialize();
	}	

/*部门管理部分开始*/	

	public function deptList(){
		//设置面包导航，主加载器请配置
		$bread=array(
			'0'=>array(
				'name'=>'系统设置',
				'url'=>U('Cms/User/#')
			),
			'1'=>array(
				'name'=>'部门列表',
				'url'=>U('Cms/Dept/deptList')
			)
		);
		$this->assign('breadhtml',$this->getBread($bread));
		//绑定搜索条件与分页
		$m=M('dept',"t_");
		$p=$_GET['p']?$_GET['p']:1;
	/*搜索功能*/
		$name=I('name')?I('name'):'';
		if($name){
			$map['name']=array('like',"%$name%");
			$map['dept_no']=array('like',"%$name%");
			$map['_logic'] = 'OR';
			$this->assign('name',$name);
		}

		$psize=self::$CMS['set']['pagesize']?self::$CMS['set']['pagesize']:20;
		//$map['is_del'] = 0;
		$cache=$m->where($map)->page($p,$psize)->select();
		//dump($cache);
		//die;
		$count=$m->where($map)->count();
		$this->getPage($count, $psize, 'Ystech-loader', '部门列表','Ystech-search');
		$this->assign('cache',$cache);		
		$this->display();
	}
	
	public function deptSet(){
		$id=I('id');
		$m=M('dept',"t_");
		//dump($m);
		//设置面包导航，主加载器请配置		
		$bread=array(
			'0'=>array(
				'name'=>'系统设置',
				'url'=>U('Cms/User/#')
			),
			'1'=>array(
				'name'=>'部门列表',
				'url'=>U('Cms/Dept/deptList')
			),
			'1'=>array(
				'name'=>'部门编辑',
				'url'=>U('Cms/Dept/deptSet',array('id'=>$id))
			)
		);
		$this->assign('breadhtml',$this->getBread($bread));
		//处理POST提交	
		if(IS_POST){
			//die('aa');
			$data=I('post.');
			// var_dump($data);
			//die;
			/*部门编号*/
			if($data['dept_no']) {
				//dump($data['dept_no']);
				$wh = array('dept_no'=>$data['dept_no']);
				if($id) $wh['id'] = array('neq',$id);
				if($m->where($wh)->count()){
					$info['status']=0;
					$info['msg']='部门编号重复！';
					$this->ajaxReturn($info);
				}
			}			
			
			if($id){
				unset($data['dept_name']);
				$re=$m->save($data);
				if(FALSE!==$re){
					$info['status']=1;
					$info['msg']='设置成功！';
				}else{
					$info['status']=0;
					$info['msg']='设置失败！';
				}
			}else{
				unset($data['id']);
				unset($data['dept_name']);
				//$data['CreateTime'] = date("Y-m-d H:i:s");
				$re=$m->add($data);//echo D()->_sql();
				if($re){
					$info['status']=1;
					$info['msg']='设置成功！';
				}else{
					$info['status']=0;
					$info['msg']='设置失败！';
				}
			}
			$this->ajaxReturn($info);
		}
		
		//处理编辑界面
		if($id){			
			$cache=$m->where('id='.$id)->find();
			$this->assign('cache',$cache);	
		}
		$model = M('dept_grade','t_');
	    $list = $model -> where('is_del=0') -> order('sort asc') -> getField('id,name');
		$this->assign('dept_grade_list',$list);
		$this->display();
	}

/*启用 ====> 禁用*/	
	public function deptDel(){
		$id=$_GET['id'];//必须使用get方法
		//dump($id);
		$m=M('dept',"t_");
		if(!$id){
			$info['status']=0;
			$info['msg']='ID不能为空!';
			$this->ajaxReturn($info);
		}
		$ids = explode(",",$id);
		//dump($ids);
		$re=$m->where(array('id'=>array('in',$ids)))->save(array('is_del'=>1));
		if($re){
			$info['status']=1;
			$info['msg']='禁用成功!';
		}else{
			$info['status']=0;
			$info['msg']='禁用失败!';
		}
		$this->ajaxReturn($info);
	}

/*禁用 ====> 启用*/	
	public function deptReback(){
		$id=$_GET['id'];//必须使用get方法
		$m=M('dept',"t_");
		if(!$id){
			$info['status']=0;
			$info['msg']='ID不能为空!';
			$this->ajaxReturn($info);
		}
		$ids = explode(",",$id);
		$re=$m->where(array('id'=>array('in',$ids)))->save(array('is_del'=>0));
		if($re){
			$info['status']=1;
			$info['msg']='启用成功!';
		}else{
			$info['status']=0;
			$info['msg']='启用失败!';
		}
		$this->ajaxReturn($info);
	}

/*部门管理部分结束*/	

    public function winpop() {
		$node = M("dept",'t_');
		$menu = array();
		$menu = $node -> where('is_del=0') -> field('id,pid,name') -> order('sort asc') -> select();
       //dump($menu);
		$tree = list_to_tree($menu);
       //dump($tree);
		$this -> assign('menu', popup_tree_menu($tree));
		$pid = array();
		$this -> assign('pid', $pid);
		echo 1234567;
		$this -> display("winpop");
	}
	
}